package com.todo.content.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.common.utils.StringUtils;
import com.todo.content.infrastructure.entity.condition.ContentClassifyCondition;
import com.todo.content.infrastructure.entity.po.ContentClassifyPO;
import com.todo.content.infrastructure.mapper.ContentClassifyMapper;
import com.todo.content.infrastructure.repository.ContentClassifyRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (ContentClassify)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("contentClassifyRepository")
public class ContentClassifyRepositoryImpl implements ContentClassifyRepository {
    @Resource
    private ContentClassifyMapper contentClassifyMapper;


    @Override
    public Boolean insertContentClassifyPO(ContentClassifyPO contentClassifyPO) {
        if (contentClassifyPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(contentClassifyPO)) {
            return false;
        }
        return contentClassifyMapper.insertContentClassifyPO(contentClassifyPO) > 0;
    }

    @Override
    public Boolean updateContentClassifyPOById(ContentClassifyPO contentClassifyPO) {
        if (contentClassifyPO == null || contentClassifyPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(contentClassifyPO)) {
            return false;
        }
        return contentClassifyMapper.updateContentClassifyPOById(contentClassifyPO) > 0;
    }

    @Override
    public Boolean removeContentClassifyPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentClassifyPO contentClassifyPO = queryContentClassifyPOById(id);
        if (contentClassifyPO == null) {
            return true;
        }
        return contentClassifyMapper.removeContentClassifyPOById(id) > 0;
    }

    @Override
    public ContentClassifyPO queryContentClassifyPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return contentClassifyMapper.queryContentClassifyPOById(id);
    }

    @Override
    public PageResult<ContentClassifyPO> queryContentClassifyByCondition(ContentClassifyCondition contentClassifyCondition) {
        PageResult<ContentClassifyPO> pageResult = new PageResult<>();
        if (contentClassifyCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(contentClassifyCondition.getPageIndex());
        pageResult.setPageSize(contentClassifyCondition.getPageSize());
        long count = queryCountByCondition(contentClassifyCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, contentClassifyCondition.getPageSize()));
        pageResult.setTotal(count);
        List<ContentClassifyPO> sightPoiPOList = contentClassifyMapper.queryContentClassifyByCondition(contentClassifyCondition);
        if (CollectionUtils.isEmpty(sightPoiPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(sightPoiPOList);
        pageResult.setCurrentIndex(sightPoiPOList.get(0).getId());
        pageResult.setHasNext(sightPoiPOList.size() >= contentClassifyCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(ContentClassifyCondition contentClassifyCondition) {
        if (contentClassifyCondition == null || contentClassifyCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return contentClassifyMapper.queryCountByCondition(contentClassifyCondition);
    }

    @Override
    public ContentClassifyPO queryContentClassifyPOByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        return contentClassifyMapper.queryContentClassifyPOByName(name);
    }

    @Override
    public ContentClassifyPO queryContentClassifyPOByCode(String classifyCode) {
        if (StringUtils.isBlank(classifyCode)) {
            return null;
        }
        return contentClassifyMapper.queryContentClassifyPOByCode(classifyCode);
    }

    @Override
    public List<ContentClassifyPO> queryContentClassifyByIdList(List<Long> classifyIdList) {
        if (CollectionUtils.isEmpty(classifyIdList)) {
            return Collections.emptyList();
        }
        return contentClassifyMapper.queryContentClassifyByIdList(classifyIdList);
    }

    @Override
    public Boolean enableContentClassifyById(Long classifyId, Integer enable) {
        if (classifyId == null || enable == null) {
            return false;
        }
        return contentClassifyMapper.enableContentClassifyById(classifyId, enable);
    }

    private boolean baseFieldCheck(ContentClassifyPO contentClassifyPO) {
        if (contentClassifyPO == null) {
            return false;
        }
        if (contentClassifyPO.getName() == null || contentClassifyPO.getCode() == null || contentClassifyPO.getBusinessScene() == null) {
            log.warn("ContentClassifyRepository#baseFieldCheck 基础校验完成，核心数据缺失 保存失败 contentClassifyPO:【{}】", JSONObject.toJSONString(contentClassifyPO));
            return false;
        }
        if (contentClassifyPO.getLastUpdateUser() == null || contentClassifyPO.getIsEnable() == null) {
            log.warn("ContentClassifyRepository#baseFieldCheck 基础校验完成，基础数据缺失 保存失败 contentClassifyPO:【{}】", JSONObject.toJSONString(contentClassifyPO));
            return false;
        }
        return true;
    }
}
